package org.game.msgBuilder;

import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.cfg.TaskCfg;
import org.cfg.cache.TaskCfgCache;
import org.constant.TaskConstant;
import org.game.db.bean.Player;
import org.game.db.bean.Task;
import org.game.protobuf.s2c.S2CShareMsg.ChangeType;
import org.game.protobuf.s2c.S2CTaskMsg;
import org.game.protobuf.s2c.S2CTaskMsg.TaskInfo;

import com.google.common.collect.Lists;
import com.xiaoleilu.hutool.util.RandomUtil;

/**
 * @author : DengYing
 * @CreateDate : 2017年8月15日 下午4:21:17
 * @Description ：任务消息封装
 */
public class TaskMsgBuilder {
	
	public static TaskInfo createTaskInfo(Player player, ChangeType changeType, Task task) {
		TaskInfo.Builder b = TaskInfo.newBuilder();
		b.setCt(changeType);
		b.setId(String.valueOf(task.getId()));
		b.setCid(task.getCid());
		b.setProgress(task.getProgress());
		b.setStatus(task.getStatus());
		return b.build();
	}
	
	public static TaskInfo createFinishTaskInfo(Player player, ChangeType changeType, TaskCfg cfg) {
		TaskInfo.Builder b = TaskInfo.newBuilder();
		b.setCt(changeType);
		b.setId(RandomUtil.randomUUID());
		b.setCid(cfg.getId());
		b.setProgress(cfg.getProgress());
		b.setStatus(TaskConstant.STATUS_RECEIVE);
		return b.build();
	}
	
	
	
	public static List<TaskInfo> createTaskInfoList(Player player,ChangeType type,List<Task> tasks){
		List<TaskInfo> taskInfos = Lists.newArrayList();
		for (Task task : tasks) {
			taskInfos.add(createTaskInfo(player,type,task));
		}
		return taskInfos;
	}

	public static S2CTaskMsg.RespTasks createRespTasks(Player player,ChangeType type){
		S2CTaskMsg.RespTasks.Builder builder = S2CTaskMsg.RespTasks.newBuilder();
		List<Task> tasks = player.getTasksFkPlayerId();
		builder.addAllTaks(createTaskInfoList(player,type,tasks));
		
		// 完成任务封装
		Map<Integer,Integer> finishTasks = player.getPlayerDatasFkPlayerId().getPlayerDataProxy().getFinishTasks();
		for (Entry<Integer, Integer> e : finishTasks.entrySet()) {
			TaskCfg cfg = TaskCfgCache.me().getById(e.getKey());
			if (cfg != null && cfg.getResetType() == TaskConstant.RESET_NO) {
				builder.addTaks(createFinishTaskInfo(player, type, cfg));		
			}
		}
		return builder.build();
	}
}
